
*-------------------------------------------------------------------------------
* suburbs population data
*-------------------------------------------------------------------------------

import delimited "data/inputs/other/il cities population.csv", clear 

gen keep = 1 if name == "Aurora city" | ///
			    name == "Joliet city" | ///
				name == "Naperville city" | ///
				(name == "Waukegan city" & county == 0) | ///
				name == "Bolingbrook village" | ///
				name == "Elgin city" | ///
				(name == "Cicero town" & county == 0) | ///
				name == "Schaumburg village" | ///
				(name == "Evanston city" & county == 0) | ///
				(name == "Arlington Heights village" & county == 0) | ///
				(name == "Palatine village" & county == 0) | ///
				(name == "Oak Lawn village" & county == 0) | ///
				(name == "Berwyn city" & county == 0) | ///
				(name == "Mount Prospect village" & county == 0) | ///
				(name == "Wheaton city" & county == 0) | ///
				(name == "Tinley Park village" & county == 0) | ///
				(name == "Oak Park village" & county == 0) | ///
				(name == "Hoffman Estates village" & county == 0) | ///
				(name == "Downers Grove village" & county == 0) | ///
				(name == "Skokie village" & county == 0) | ///			
				(name == "Des Plaines city" & county == 0) | ///
				(name == "Orland Park village" & county == 0) | ///				
				(name == "Wheaton city" & county == 0) 								
				
qui su keep
assert `r(sum)' == 22

keep if keep == 1
keep name popestimate*

reshape long popestimate, i(name) j(year, string)
gen month = tm(2020m4) if year == "042020"
replace year = "01/" + year if length(year) == 4
replace month = monthly(year, "MY") if month == .
format month %tm

egen cityid = group(name)
tsset cityid month
tsfill, full

gen ln_pob = ln(pop)
by cityid: ipolate ln_pob month, gen(ln_pob_i)
gen pop_cgr = exp(ln_pob_i)

collapse (sum) pop_cgr, by(month)

rename pop_cgr pop_cgr_suburbs
tempfile pop_suburbs
savesome month pop using `pop_suburbs'


*-------------------------------------------------------------------------------
* chicago population data
*-------------------------------------------------------------------------------

* population
clear 
set obs 120
gen month = tm(2010m6) if _n == 1
replace month = month[_n-1] + 1 if month[_n-1] ~= .
format month %tm
gen pop = 2695598 if _n == 1
replace pop = 2746388 if month == tm(2020m5)

	/*
	Population, Census, April 1, 2020	2,746,388
	Population, Census, April 1, 2010	2,695,598
	*/
	
gen ln_pob = ln(pop)
ipolate ln_pob month, gen(ln_pob_i)
gen pop_cgr = exp(ln_pob_i)
tempfile pop_chicago
savesome month pop_cgr using `pop_chicago'


*-------------------------------------------------------------------------------
* traffic stop data
*-------------------------------------------------------------------------------

use "data/outputs/TSSS_beat.dta", clear

replace agencyname = "troopers" if regexm(agencyname, "ILLINOIS")
replace agencyname = "cpd" if regexm(agencyname, "CHICAGO")
replace agencyname = "suburbs" if agencyname ~= "troopers" & agencyname ~= "cpd"

collapse (sum) count* black citation reason_movingviolation reason_equip, by(agencyname month)

reshape wide count count_race black citation reason_movingviolation reason_equip, i(month) j(agencyname, string)
renvars count* black* citation*, prefix(itss)

drop if month < tm(2012m1) | month > tm(2020m2)


*-------------------------------------------------------------------------------
* merge
*-------------------------------------------------------------------------------

merge 1:1 month using `pop_chicago'
drop _m
merge 1:1 month using `pop_suburbs'
drop _m

*-------------------------------------------------------------------------------
* calculate quantities for graphs
*-------------------------------------------------------------------------------

* citation rate
gen citationrate_cpd = itsscitationcpd / itsscountcpd
gen citationrate_suburbs = itsscitationsuburbs / itsscountsuburbs

* equipment rate
gen equiprate_cpd = reason_equipcpd / itsscountcpd
gen equiprate_suburbs = reason_equipsuburbs / itsscountsuburbs
	   
* treatment area	   
gen treatment = 1 if (month >= tm(2015m8) & month <= tm(2016m1))


*-------------------------------------------------------------------------------
* graphs
*-------------------------------------------------------------------------------

* citation rate	
#delimit;
twoway (area treatment month if month >= tm(2012m1) & month <= tm(2020m2), 
lcolor(gs15%100) fcolor(gs15%100))
(connected citationrate_suburbs month if month >= tm(2012m1) & month <= tm(2020m3),
lcolor(gs8) mcolor(gs8) msize(small) msymbol(square))
(connected citationrate_cpd month if month >= tm(2012m1) & month <= tm(2020m3),
lcolor(blue) mcolor(blue) msize(small)),
graphregion(fcolor(white) lcolor(white) margin(zero))
plotregion(fcolor(white) lstyle(none) lcolor(white) ilstyle(none))
xsize(7) ysize(5)
title("", 
	  color(black) placement(west) justification(left) size(medlarge)) 
ytitle("Proportion of traffic stops producing a citation", size(medlarge))
yscale(lcolor(none))
ylabel(0(.2)1, labsize(large) glcolor(white) angle(horizontal) axis(1))
xtitle("", size(medsmall)) 
xscale(lcolor(none) range(636 722))
xlabel(624(12)720, labsize(medlarge) format(%tmCY)) 
legend(order(3 "Chicago Police" 2 "Suburbs")
ring(0) pos(2) rows(2) symxsize(*.25));
graph export "figures/FigureS4a.pdf", replace;

* equipment rate	
#delimit;
twoway (area treatment month if month >= tm(2012m1) & month <= tm(2020m2), 
lcolor(gs15%100) fcolor(gs15%100))
(connected equiprate_suburbs month if month >= tm(2012m1) & month <= tm(2020m3),
lcolor(gs8) mcolor(gs8) msize(small) msymbol(square))
(connected equiprate_cpd month if month >= tm(2012m1) & month <= tm(2020m3),
lcolor(blue) mcolor(blue) msize(small)),
graphregion(fcolor(white) lcolor(white) margin(zero))
plotregion(fcolor(white) lstyle(none) lcolor(white) ilstyle(none))
xsize(7) ysize(5)
title("", 
	  color(black) placement(west) justification(left) size(medlarge)) 
ytitle("Proportion of traffic stops for equipment", size(medlarge))
yscale(lcolor(none))
ylabel(0(.2)1, labsize(large) glcolor(white) angle(horizontal) axis(1))
xtitle("", size(medsmall)) 
xscale(lcolor(none) range(636 722))
xlabel(624(12)720, labsize(medlarge) format(%tmCY)) 
legend(order(3 "Chicago Police" 2 "Suburbs")
ring(0) pos(2) rows(2) symxsize(*.25));
graph export "figures/FigureS4b.pdf", replace;
	
	
* End
